<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>033-Android存储（8）：DBFlow使用详解</title>
  
  
  <meta property="og:title" content="033-Android存储（8）：DBFlow使用详解">
  
  
  <meta property="og:url" content="http://example.com/2019/10/19/033-Android%E5%AD%98%E5%82%A8%EF%BC%888%EF%BC%89%EF%BC%9ADBFlow%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2019-10-19">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Android">
  
  <meta property="og:article:tag" content="Android持久化">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      033-Android存储（8）：DBFlow使用详解
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2019-10-19T04:22:11.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2019-10-19</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Android/" class="post-meta-link">Android</a>
    
    
    
    <span class="dot"></span>
    <span>1.3k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Android/" class="post-meta-link">Android</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Android持久化/" class="post-meta-link">Android持久化</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <p><a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/article/details/114592859">【5年Android从零复盘系列之三十三】Android存储（8）：DBFlow使用详解</a></p>
<hr />

<p><strong>【代码是最好的老师】</strong></p>
<h1 id="1-概述"><a href="#1-概述" class="headerlink" title="1.概述"></a>1.概述</h1><p><a target="_blank" rel="noopener" href="https://github.com/agrosner/DBFlow">GitHub直达DBFlow</a></p>
<p>DBFlow是一个注解化(AnnotationProcessing)的ORM框架：</p>
<ol>
<li>同Springboot一样的编码思维，让开发更专注于逻辑而不是死板的db代码</li>
<li>支持同步/异步/事务操作</li>
<li>支持自定义数据库储放路径</li>
<li>使用灵活、简洁，可读性高</li>
<li>编译期代码生成，运行时对性能是零损耗的，不逊于其他数据库框架</li>
<li>支持懒加载</li>
<li>【大招】可以直接注解化生成content provider<a target="_blank" rel="noopener" href="https://github.com/SimonVT/schematic">依赖Schematic库</a></li>
</ol>
<h1 id="2-关于"><a href="#2-关于" class="headerlink" title="2.关于"></a>2.关于</h1><p>DBFlow的灵魂精髓在于，严格使用注解生成SQLiteDataBase/SQLiteOpenHelper等基础代码。</p>
<p>既简化了代码编写，同时又不损失性能（编译时生成中间代码、非运行时反射）</p>
<p>注解快捷生成ContentProvider是DBFlow的另一大精髓，DBFlow是Android敏捷开发的示范框架</p>
<h1 id="3-配置"><a href="#3-配置" class="headerlink" title="3.配置"></a>3.配置</h1><p>本教程有缺失或过时，请参考<br><a target="_blank" rel="noopener" href="https://agrosner.gitbooks.io/dbflow/content/">官方文档教程</a></p>
<p>step1：在项目根目录的build.gradle中添加jitpack源</p>
<pre><code>maven &#123; url &quot;https://www.jitpack.io&quot; &#125;
</code></pre>
<p>如（dbflow step1-1、dbflow step1-2）：</p>
<pre><code>buildscript &#123;
    repositories &#123;
        google()
        jcenter()
        //dbflow step1-1
        maven &#123; url &quot;https://www.jitpack.io&quot; &#125;
    &#125;
    dependencies &#123;
        classpath &quot;com.android.tools.build:gradle:4.0.2&quot;
    &#125;
&#125;
allprojects &#123;
    repositories &#123;
        google()
        jcenter()
        //dbflow step1-2
        maven &#123; url &quot;https://www.jitpack.io&quot; &#125;
    &#125;
&#125;
</code></pre>
<p>step2:配置混淆规则</p>
<p>按使用版本更新混淆规则，参考<a target="_blank" rel="noopener" href="https://github.com/agrosner/DBFlow">GitHub DBFlow</a></p>
<pre><code># dbflow step2
# 5.0-
 -keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder &#123; *; &#125;
# 5.0+ btw:5.0后变更了包名
# -keep class * extends com.dbflow5.config.DatabaseHolder &#123; *; &#125;
</code></pre>
<p>step3:module导入依赖库</p>
<pre><code>//定义依赖库版本号
def dbflow_version = &quot;4.2.4&quot;
def sqlcipher_version = &quot;3.4.0&quot;
android&#123;
    ...
&#125;
dependencies &#123;
...
//dbflow begin
//==============================================================================
annotationProcessor &quot;com.github.Raizlabs.DBFlow:dbflow-processor:$dbflow_version&quot;
implementation &quot;com.github.Raizlabs.DBFlow:dbflow-core:$dbflow_version&quot;
implementation &quot;com.github.Raizlabs.DBFlow:dbflow:$dbflow_version&quot;
//==============================================================================
//dbflow end
</code></pre>
<p>}</p>
<p>配置完，sync</p>
<h1 id="4-使用"><a href="#4-使用" class="headerlink" title="4.使用"></a>4.使用</h1><p>step4:初始化配置</p>
<p>app的application中init</p>
<pre class="highlight"><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BDFlowApplication</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate();</span><br><span class="line">        DBManager.init(<span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>

<p>项目采用组件化，故多封装了一层</p>
<pre class="highlight"><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DBManager</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">init</span><span class="params">(Context context)</span></span>&#123;</span><br><span class="line">        FlowManager.init(context);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>

<p>记得AndroidManifest.xml中配置,否则不会初始化DBFlow</p>
<pre class="highlight"><span class="line">&lt;application</span><br><span class="line">        android:name=&quot;.BDFlowApplication&quot;</span><br><span class="line">        ...</span><br></pre>


<h2 id="4-1-创建数据库"><a href="#4-1-创建数据库" class="headerlink" title="4.1 创建数据库"></a>4.1 创建数据库</h2><p>使用注解@Database声明数据库配置类。</p>
<p>@Database(name = AppDB.DB_NAME ,version =  AppDB.DB_VERSION)配置数据库名称、版本号</p>
<pre class="highlight"><span class="line"><span class="meta">@Database(name = AppDB.DB_NAME ,version =  AppDB.DB_VERSION)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AppDB</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String DB_NAME =<span class="string">&quot;AppDB&quot;</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> DB_VERSION =<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>创建</p>
<pre class="highlight"><span class="line"></span><br></pre>


<h2 id="4-2-创建表（设计表）"><a href="#4-2-创建表（设计表）" class="headerlink" title="4.2 创建表（设计表）"></a>4.2 创建表（设计表）</h2><p>继承BaseModel并使用注解@Table声明实体类为数据库对应的表，然后build project ，生成中间代码。</p>
<p>使用注解@PrimaryKey(autoincrement = true),声明变量为主键，同时设置自增长。</p>
<p>使用@Column声明变量为数据列</p>
<pre class="highlight"><span class="line"><span class="meta">@Table(database = AppDB.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">BaseModel</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PrimaryKey(autoincrement = true)</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">long</span> id ;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Column</span></span><br><span class="line">    <span class="keyword">private</span> String account ;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Column</span></span><br><span class="line">    <span class="keyword">private</span> String password;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Column</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Column</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Column</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">long</span> loginMill;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">    <span class="comment">//setter &amp; getter</span></span><br></pre>

<p>组件化跨模块帮助类：</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.func_db_dbflow.db_main.crud;</span><br><span class="line"><span class="keyword">import</span> android.util.Log;</span><br><span class="line"><span class="keyword">import</span> com.cupster.func_db_dbflow.db_main.table.User;</span><br><span class="line"><span class="keyword">import</span> com.cupster.func_db_dbflow.db_main.table.User_Table;</span><br><span class="line"><span class="keyword">import</span> com.raizlabs.android.dbflow.sql.language.SQLite;</span><br><span class="line"><span class="keyword">import</span> com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;</span><br><span class="line"><span class="keyword">import</span> com.raizlabs.android.dbflow.structure.database.transaction.Transaction;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> androidx.annotation.NonNull;</span><br><span class="line"><span class="keyword">import</span> androidx.annotation.Nullable;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserHelper</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">insert</span><span class="params">(String name)</span></span>&#123;</span><br><span class="line">        User user = <span class="keyword">new</span> User();</span><br><span class="line">        user.name = name ;</span><br><span class="line">        user.age = <span class="number">27</span>;</span><br><span class="line">        user.account =<span class="string">&quot;18960&quot;</span>;</span><br><span class="line">        user.pwd =<span class="string">&quot;123&quot;</span>;</span><br><span class="line">        user.money = <span class="number">5000.0</span>;</span><br><span class="line">        <span class="keyword">return</span> user.save();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//增删改查同理字段筛选</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">(String newName,String name )</span></span>&#123;</span><br><span class="line">        SQLite.update(User.class)</span><br><span class="line">                .set(User_Table.name.eq(newName))</span><br><span class="line">                .where(User_Table.name.eq(name))</span><br><span class="line">                .execute();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span>  <span class="title">updateOld</span><span class="params">(String newName,String name )</span></span>&#123;</span><br><span class="line">        User user = SQLite.select()</span><br><span class="line">                .from(User.class)</span><br><span class="line">                .where(User_Table.name.is(name))</span><br><span class="line">                .querySingle();</span><br><span class="line">        <span class="keyword">if</span> (user != <span class="keyword">null</span>)&#123;</span><br><span class="line">            user.name = newName;</span><br><span class="line">            <span class="keyword">return</span> user.update();</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">delete</span><span class="params">(String name )</span></span>&#123;</span><br><span class="line">        SQLite.delete(User.class)</span><br><span class="line">                .where(User_Table.name.is(name))</span><br><span class="line">                .execute();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//单个查询</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">selectUser</span><span class="params">()</span></span>&#123;</span><br><span class="line">        User user = SQLite.select().from(User.class)</span><br><span class="line">                .where(User_Table.name.is(<span class="string">&quot;Cupster&quot;</span>)).querySingle();</span><br><span class="line">        <span class="keyword">return</span> user.name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//多个查询</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">selectUserList</span><span class="params">()</span></span>&#123;</span><br><span class="line">        List&lt;User&gt; userList = SQLite.select().from(User.class)</span><br><span class="line">                .where(User_Table.name.like(<span class="string">&quot;C%&quot;</span>)).queryList();</span><br><span class="line">        <span class="keyword">return</span> userList.toString();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//单个 异步查询</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectUserAsync</span><span class="params">()</span></span>&#123;</span><br><span class="line">        SQLite.select().from(User.class)</span><br><span class="line">                .where(User_Table.name.is(<span class="string">&quot;Cupster&quot;</span>)).async().querySingleResultCallback(<span class="keyword">new</span> QueryTransaction.QueryResultSingleCallback&lt;User&gt;() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onSingleQueryResult</span><span class="params">(QueryTransaction transaction, <span class="meta">@Nullable</span> User user)</span> </span>&#123;</span><br><span class="line">                        <span class="keyword">if</span> (user != <span class="keyword">null</span>)&#123;</span><br><span class="line">                            Log.i( <span class="string">&quot;onSingleQueryResult: &quot;</span>,<span class="string">&quot;query name : &quot;</span>+user.name);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;).error(<span class="keyword">new</span> Transaction.Error() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(<span class="meta">@NonNull</span> Transaction transaction, <span class="meta">@NonNull</span> Throwable error)</span> </span>&#123;</span><br><span class="line">                        Log.i( <span class="string">&quot;onError: &quot;</span>,<span class="string">&quot;&quot;</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;).success(<span class="keyword">new</span> Transaction.Success() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onSuccess</span><span class="params">(<span class="meta">@NonNull</span> Transaction transaction)</span> </span>&#123;</span><br><span class="line">                        Log.i( <span class="string">&quot;onSuccess: &quot;</span>,transaction.name());</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;).execute();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//数据库升级</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>




<h1 id="5-其他"><a href="#5-其他" class="headerlink" title="5.其他"></a>5.其他</h1><p>数据库升级</p>
<h2 id="5-1新增表"><a href="#5-1新增表" class="headerlink" title="5.1新增表"></a>5.1新增表</h2><p>仅需编写新增表类，数据库版本号+1，编译、运行</p>
<h2 id="5-2新增表的列项"><a href="#5-2新增表的列项" class="headerlink" title="5.2新增表的列项"></a>5.2新增表的列项</h2><p>继承 类 AlterTableMigration 用于重命名表，增加列</p>
<p>继承 类 IndexMigration/IndexPropertyMigration 用于索引创建和删除</p>
<p>继承 类 UpdateTableMigration 升级数据库的时候更新数据</p>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210309170615_IWzeno.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/uPic/20210309170615_IWzeno.png"/>


<br/>
<br/>
<br/>

<p>升级表，添加数据列</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.func_db_dbflow.db_main.alter;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.cupster.func_db_dbflow.db_main.AppDB;</span><br><span class="line"><span class="keyword">import</span> com.cupster.func_db_dbflow.db_main.table.User;</span><br><span class="line"><span class="keyword">import</span> com.raizlabs.android.dbflow.annotation.Migration;</span><br><span class="line"><span class="keyword">import</span> com.raizlabs.android.dbflow.sql.SQLiteType;</span><br><span class="line"><span class="keyword">import</span> com.raizlabs.android.dbflow.sql.migration.AlterTableMigration;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Migration(version = AppDB.DB_VERSION, database = AppDB.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AddSingleName</span> <span class="keyword">extends</span> <span class="title">AlterTableMigration</span>&lt;<span class="title">User</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AddSingleName</span><span class="params">(Class&lt;User&gt; table)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(table);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPreMigrate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        addColumn(SQLiteType.TEXT ,<span class="string">&quot;single_name&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>


<p><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/15685038">代码demo</a></p>

  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2019/10/19/033-Android%E5%AD%98%E5%82%A8%EF%BC%888%EF%BC%89%EF%BC%9ADBFlow%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/">http://example.com/2019/10/19/033-Android%E5%AD%98%E5%82%A8%EF%BC%888%EF%BC%89%EF%BC%9ADBFlow%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2019/10/22/034-Android存储（9）：腾讯MMKV 高性能键值存储组件详解/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">034-Android存储（9）：腾讯MMKV 高性能键值存储组件详解 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2019/10/16/032-Android存储（7）：LitePal使用详解/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">032-Android存储（7）：LitePal使用详解 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%85%B3%E4%BA%8E"><span class="toc-text">2.关于</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E9%85%8D%E7%BD%AE"><span class="toc-text">3.配置</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8"><span class="toc-text">4.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-text">4.1 创建数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%88%9B%E5%BB%BA%E8%A1%A8%EF%BC%88%E8%AE%BE%E8%AE%A1%E8%A1%A8%EF%BC%89"><span class="toc-text">4.2 创建表（设计表）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%85%B6%E4%BB%96"><span class="toc-text">5.其他</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1%E6%96%B0%E5%A2%9E%E8%A1%A8"><span class="toc-text">5.1新增表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2%E6%96%B0%E5%A2%9E%E8%A1%A8%E7%9A%84%E5%88%97%E9%A1%B9"><span class="toc-text">5.2新增表的列项</span></a></li></ol></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%85%B3%E4%BA%8E"><span class="toc-text">2.关于</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E9%85%8D%E7%BD%AE"><span class="toc-text">3.配置</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8"><span class="toc-text">4.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-text">4.1 创建数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%88%9B%E5%BB%BA%E8%A1%A8%EF%BC%88%E8%AE%BE%E8%AE%A1%E8%A1%A8%EF%BC%89"><span class="toc-text">4.2 创建表（设计表）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%85%B6%E4%BB%96"><span class="toc-text">5.其他</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1%E6%96%B0%E5%A2%9E%E8%A1%A8"><span class="toc-text">5.1新增表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2%E6%96%B0%E5%A2%9E%E8%A1%A8%E7%9A%84%E5%88%97%E9%A1%B9"><span class="toc-text">5.2新增表的列项</span></a></li></ol></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%85%B3%E4%BA%8E"><span class="toc-text">2.关于</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E9%85%8D%E7%BD%AE"><span class="toc-text">3.配置</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8"><span class="toc-text">4.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-text">4.1 创建数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%88%9B%E5%BB%BA%E8%A1%A8%EF%BC%88%E8%AE%BE%E8%AE%A1%E8%A1%A8%EF%BC%89"><span class="toc-text">4.2 创建表（设计表）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%85%B6%E4%BB%96"><span class="toc-text">5.其他</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1%E6%96%B0%E5%A2%9E%E8%A1%A8"><span class="toc-text">5.1新增表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2%E6%96%B0%E5%A2%9E%E8%A1%A8%E7%9A%84%E5%88%97%E9%A1%B9"><span class="toc-text">5.2新增表的列项</span></a></li></ol></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>